package api

import (
	activity "api-gateway/basic/activity_proto"
	"api-gateway/basic/inits"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"github.com/gin-gonic/gin"
)

// ListGroupPing
// @Summary      拼团商品列表
// @Description  拼团商品列表
// @Tags         拼团模块
// @Accept       json
// @Produce      json
// @Param        HotSearch  body      request.GroupPGoodsList  true  "首页参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /groupPing/ListGroupPing [post]
func ListGroupPing(c *gin.Context) {
	var req request.GroupPGoodsList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	goodsDetail, err := inits.ActivityClient.GroupPGoodsList(c, &activity.GroupPGoodsListReq{
		Page:     int64(req.Page),
		PageSize: int64(req.PageSize),
	})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "拼团商品列表展示成功", goodsDetail)
}

// CreateGroupP
// @Summary      创建拼团
// @Description  创建拼团
// @Tags         拼团模块
// @Accept       json
// @Produce      json
// @Param        HotSearch  body      request.CreateGroupP  true  "首页参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /groupPing/CreateGroupP [post]
func CreateGroupP(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.CreateGroupP
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	create, err := inits.ActivityClient.CreateGroupP(c, &activity.CreateGroupPReq{
		GroupGid:  req.GroupGid,
		Num:       req.Num,
		UserId:    int64(userId),
		GroupType: req.GroupType,
	})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
	}
	response.Success(c, "创建拼团成功", create)
}

// JoinGroupP
// @Summary      参团
// @Description  参团
// @Tags         拼团模块
// @Accept       json
// @Produce      json
// @Param        HotSearch  body      request.JoinGroupP  true  "首页参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /groupPing/JoinGroupP [post]
func JoinGroupP(c *gin.Context) {
	var req request.JoinGroupP
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	join, err := inits.ActivityClient.JoinGroupP(c, &activity.JoinGroupPReq{
		GroupOrderNo: req.GroupOrderNo,
		InviteCode:   req.InviteCode,
		Num:          req.Num,
		UserId:       req.UserId,
	})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "加入拼团成功", join)
}

// GetGroupDetail
// @Summary      获取拼团详情
// @Description  获取拼团详情
// @Tags         拼团模块
// @Accept       json
// @Produce      json
// @Param        HotSearch  query      request.GroupDetail  true  "首页参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /groupPing/JoinGroupP [get]
func GetGroupDetail(c *gin.Context) {
	var req request.GroupDetail
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	detail, err := inits.ActivityClient.GetGroupDetail(c, &activity.GroupDetailReq{
		GroupId: req.GroupId,
	})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	response.Success(c, "拼团详情展示成功", detail)
}

// UserGroupRecords
// @Summary      获取用户拼团记录
// @Description  获取用户拼团记录
// @Tags         拼团模块
// @Accept       json
// @Produce      json
// @Param        HotSearch  query      request.UserGroupRecords  true  "首页参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /groupPing/UserGroupRecords [get]
func UserGroupRecords(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.UserGroupRecords
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	records, err := inits.ActivityClient.UserGroupRecords(c, &activity.UserGroupRecordsReq{
		UserId:   int64(userId),
		Page:     int64(req.Page),
		PageSize: int64(req.PageSize),
	})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
	}
	response.Success(c, "用户拼团记录展示成功", records)
}
